<template>
  <div class="student_exam_paper_read student_exam_paper_do">
    <el-container  class="app-item-contain">
      <div class="app-item-contain_l">
        <div>试卷名称:{{form.name}}</div>
        <p class="question-title">试卷总分：{{answer.score}}</p>
        <p class="question-title">考试时间：{{form.suggestTime}}分钟</p>

        <el-row  class="do-exam-title">
          <el-col :span="24">
            <span class="do-exam-time-tag" :key="item.itemOrder"  v-for="item in answer.answerItems">
              <el-tag :type="questionCompleted(item.completed)" class="do-exam-title-tag" @click="goAnchor('#question-'+item.itemOrder)">{{item.itemOrder}}</el-tag>
            </span>
            <div class="do-exam-time">
              <p>剩余时间：</p>
              <p>{{formatSeconds(remainTime)}}</p>
            </div>
          </el-col>
        </el-row>

        <el-row class="do-align-center">
          <el-button type="primary" @click="submitForm">提交试卷</el-button>
        </el-row>
      </div>

      <div class="app-item-contain_r">
          <el-form :model="form" ref="form" v-loading="formLoading" label-width="100px">
              <div class="c-flex c-dis-flex-col c-width">
                <el-row :key="index"  v-for="(titleItem,index) in form.titleItems">
                  <h3>{{titleItem.name}}</h3>
                  <div class="exampaper-item-box" v-if="titleItem.questionItems.length!==0">
                    <el-form-item :key="questionItem.itemOrder" :label="questionItem.itemOrder+'.'"
                                  v-for="questionItem in titleItem.questionItems"
                                  class="exam-question-item" label-width="50px" :id="'question-'+ questionItem.itemOrder">
                      <QuestionEdit :qType="questionItem.questionType" :question="questionItem"
                                    :answer="answer.answerItems[questionItem.itemOrder-1]"/>
                    </el-form-item>
                  </div>
                </el-row>
              </div>

          </el-form>
      </div>

    </el-container>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
import { formatSeconds } from '@/utils'
import QuestionEdit from '../components/QuestionEdit'
import { PaperSelectByExamId } from '@/api/examPaper'
import { answerSubmit,findAnswerCount } from '@/api/examPaperAnswer'

export default {
  components: { QuestionEdit },
  data () {
    return {
      form: {},
      formLoading: false,
      answer: {
        questionId: null,
        doTime: 0,
        answerItems: [],
        examMngId:null
      },
      timer: null,
      remainTime: 0
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      // 验证考试次数
      findAnswerCount(id).then(res =>{
        // 失败时
        if (res.code != 200) {
          _this.formLoading = false
          _this.$message.error(res.msg);
        } else {
          PaperSelectByExamId(id).then(re => {
            _this.form = re.data
            _this.remainTime = re.data.suggestTime * 60
            _this.initAnswer()
            _this.timeReduce()
            _this.formLoading = false
          })
        }
      })
    }
  },
  mounted () {

  },
  beforeDestroy () {
    window.clearInterval(this.timer)
  },
  methods: {
    formatSeconds (theTime) {
      return formatSeconds(theTime)
    },
    timeReduce () {
      let _this = this
      this.timer = setInterval(function () {
        if (_this.remainTime <= 0) {
          _this.submitForm()
        } else {
          ++_this.answer.doTime
          --_this.remainTime
        }
      }, 1000)
    },
    questionCompleted (status) {
      if (status == null) {
        return 'warning'
      }
      if (status) {
        return 'success'
      }
      if (!status) {
        return 'info'
      }
    },
    goAnchor (selector) {
      this.$el.querySelector(selector).scrollIntoView({ behavior: 'instant', block: 'center', inline: 'nearest' })
    },
    initAnswer () {
      this.answer.id = this.form.id
      let titleItemArray = this.form.titleItems
      for (let tIndex in titleItemArray) {
        let questionArray = titleItemArray[tIndex].questionItems
        for (let qIndex in questionArray) {
          let question = questionArray[qIndex]
          this.answer.answerItems.push({ questionId: question.id, content: null, contentArray: [], completed: false, itemOrder: question.itemOrder })
        }
      }
    },
    submitForm () {
      let _this = this
      window.clearInterval(_this.timer)
      _this.formLoading = true
      this.answer.examMngId = this.$route.query.id;
      answerSubmit(this.answer).then(re => {
        if (re.code === 200) {
          _this.$alert('试卷得分：' + re.data + '分', '考试结果', {
            confirmButtonText: '返回考试记录',
            callback: action => {
              _this.$router.push('/record/index')
            }
          })
        } else {
          _this.$message.error(re.message)
        }
        _this.formLoading = false
      }).catch(e => {
        _this.formLoading = false
      })
    }
  },
  computed: {

  }
}
</script>

<style lang="scss" scoped>
  .align-center {
    text-align: center
  }

  .exam-question-item {
    padding: 10px;

    .el-form-item__label {
      font-size: 15px !important;
    }
  }

  .question-title-padding {
    padding-left: 25px;
    padding-right: 25px;
  }
  .do-exam-title-tag {
    margin-left: 5px;
    cursor: pointer
  }

</style>
